<?php
declare (strict_types = 1);

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\facade\Db;

class TableField extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('tablefield')
            ->addArgument('connection',Argument::REQUIRED,'this table connection')
            ->addArgument('table',Argument::REQUIRED,'this table name')
            ->setDescription('the tablefield command');
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
        $link=$input->getArgument('connection');
        $table=$input->getArgument('table');

        $result=Db::connect($link)->query('SHOW FULL COLUMNS FROM '.$table);
        if ($result) {
            $rule='';
            foreach ($result as $item) {
                $val = array_change_key_case($item);
                $rule .="'{$val['field']}'=> 'require',";
            }
            $output->writeln($rule);
            $output->writeln("   ");

            $message='';
            foreach ($result as $item) {
                $val = array_change_key_case($item);
                $message .="'{$val['field']}.require'=> '{$val['field']}',";
            }
            $output->writeln($message);
            $output->writeln("   ");

            $scene='';
            foreach ($result as $item) {
                $val = array_change_key_case($item);
                $scene .="'{$val['field']}',";
            }
            $output->writeln($scene);

        }else{
            $output->writeln('数据表为空');
        }
    }
}
